package Utils;

import java.util.Date;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;

import datatypes.film.Films;
import datatypes.film.filmActors;
import datatypes.film.filmDirectors;
import datatypes.film.filmGenres;
import datatypes.film.filmRoles;
import datatypes.film.filmWriters;
import datatypes.tv.tvActors;
import datatypes.tv.tvGenres;
import datatypes.tv.tvGuestRoles;
import datatypes.tv.tvProgramCreators;
import datatypes.tv.tvPrograms;
import datatypes.tv.tvStarringRoles;
import Main.SharedPref;
import Utils.Concurrent.AsyncTask;
import Utils.Concurrent.TaskPool;
import Utils.Freebase.FB_ParsingUtils;
import Utils.UI.UIUpdater;

/**
 * Loading Center used for background loading of long operations. It uses a
 * Generic AsyncTask class, and every object of that class represents a
 * different task (loading actors for example)
 * 
 * @author micha
 * 
 */

public class LoadingCenter {

	/* ----- Loading Center API public functions ----- */

	/**
	 * load tv actors from database (currently parsing)
	 * 
	 * @param tv_actor
	 *            an actor object
	 * 
	 */
	public static tvActors Load_tvActors() {

		String filePath = SharedPref.Tv_DB_Folder + SharedPref.fileSeparator
				+ "tv_actor.tsv";

		return FB_ParsingUtils.Parse_tv_actor(filePath, -1);

	}

	public static tvActors Load_tvActors(Display display, Label label) {

		new GUILoadingTask("load_tvActors", display, label).execute();
		return null;
	}

	public static tvGuestRoles Load_tvGuestRoles() {

		String filePath = SharedPref.Tv_DB_Folder + SharedPref.fileSeparator
				+ "tv_guest_role.tsv";

		return FB_ParsingUtils.Parse_tv_guest_roles(filePath, -1);

	}

	public static tvStarringRoles Load_tvStarringRoles() {

		String filePath = SharedPref.Tv_DB_Folder + SharedPref.fileSeparator
				+ "regular_tv_appearance.tsv";

		return FB_ParsingUtils.parse_regular_tv_appearance(filePath, -1);

	}

	public static tvPrograms Load_tvPrograms() {

		String filePath = SharedPref.Tv_DB_Folder + SharedPref.fileSeparator
				+ "tv_program.tsv";

		return FB_ParsingUtils.Parse_tv_programs(filePath, -1);

	}

	public static tvPrograms Load_tvPrograms(Display display, Label label) {

		new GUILoadingTask("load_tvPrograms", display, label).execute();
		return null;
	}

	public static tvProgramCreators Load_tvProgramCreators() {
		String filePath = SharedPref.Tv_DB_Folder + SharedPref.fileSeparator
				+ "tv_program_creator.tsv";

		return FB_ParsingUtils.Parse_tv_program_creators(filePath, -1);
	}

	public static tvGenres Load_tvGenres() {
		String filePath = SharedPref.Tv_DB_Folder + SharedPref.fileSeparator
				+ "tv_genre.tsv";

		return FB_ParsingUtils.Parse_tv_genres(filePath, -1);
	}

	public static Films Load_Films() {

		String filePath = SharedPref.Film_DB_Folder + SharedPref.fileSeparator
				+ "film.tsv";

		return FB_ParsingUtils.Parse_films(filePath, -1);

	}

	public static Films Load_Films(Display display, Label label) {

		new GUILoadingTask("load_films", display, label).execute();
		return null;

	}

	public static filmActors Load_filmActors() {
		String filePath = SharedPref.Film_DB_Folder + SharedPref.fileSeparator
				+ "actor.tsv";
		return FB_ParsingUtils.Parse_film_actors(filePath, -1);
	}

	public static filmDirectors Load_filmDirectors() {
		String filePath = SharedPref.Film_DB_Folder + SharedPref.fileSeparator
				+ "director.tsv";
		return FB_ParsingUtils.Parse_film_directors(filePath, -1);
	}

	public static filmWriters Load_filmWriters() {
		String filePath = SharedPref.Film_DB_Folder + SharedPref.fileSeparator
				+ "writer.tsv";
		return FB_ParsingUtils.Parse_film_writers(filePath, -1);
	}

	public static filmRoles Load_filmRoles() {
		String filePath = SharedPref.Film_DB_Folder + SharedPref.fileSeparator
				+ "performance.tsv";
		return FB_ParsingUtils.Parse_film_roles(filePath, -1);
	}

	public static filmGenres Load_filmGenres() {
		String filePath = SharedPref.Film_DB_Folder + SharedPref.fileSeparator
				+ "film_genre.tsv";
		return FB_ParsingUtils.Parse_film_genres(filePath, -1);

	}

	public static filmActors Load_filmActors(Display display, Label label) {
		new GUILoadingTask("load_filmActors", display, label).execute();
		return null;
	}

	/* generic GUI loading task for multi functional usage */
	private static class GUILoadingTask extends AsyncTask<String> {

		private String loadingCommand;

		public GUILoadingTask(Display display, Object... vals) {
			super(display, vals);
		}

		public GUILoadingTask(String loadingCommand, Display display,
				Object... vals) {
			super(display, vals);
			this.loadingCommand = loadingCommand;
		}

		@Override
		public String doInBackground() {

			if (loadingCommand.equals("load_tvActors")) {
				Label label = (Label) vals[0];
				HandleTvActors(getDisplay(), label);
			} else if (loadingCommand.equals("load_tvPrograms")) {
				Label label = (Label) vals[0];
				HandleTvPrograms(getDisplay(), label);
			} else if (loadingCommand.equals("load_films")) {
				Label label = (Label) vals[0];
				HandleFilms(getDisplay(), label);
			} else if (loadingCommand.equals("load_filmActors")) {
				Label label = (Label) vals[0];
				HandleFilmActors(getDisplay(), label);
			}
			return null;

		}

		private void HandleFilmActors(Display display, Label label) {
			String filePath = SharedPref.Film_DB_Folder
					+ SharedPref.fileSeparator + "actor.tsv";

			long startTime = System.currentTimeMillis();
			filmActors film_actors = FB_ParsingUtils.Parse_film_actors(
					filePath, -1);
			// System.out.println(film_actors);

			UIUpdater
					.setWidgetText(
							label,
							DebugUtils.TimeToString((new Date()).getTime()
									- startTime));

		}

		private void HandleFilms(Display display, Label label) {
			String filePath = SharedPref.Film_DB_Folder
					+ SharedPref.fileSeparator + "film.tsv";

			long startTime = System.currentTimeMillis();
			Films films = FB_ParsingUtils.Parse_films(filePath, -1);
			// System.out.println(films);

			UIUpdater
					.setWidgetText(
							label,
							DebugUtils.TimeToString((new Date()).getTime()
									- startTime));

		}

	}

	/* ----- Loading Center private functions ----- */

	private static void HandleTvActors(Display display, Label label) {
		String filePath = SharedPref.Tv_DB_Folder + SharedPref.fileSeparator
				+ "tv_actor.tsv";

		long startTime = System.currentTimeMillis();
		tvActors tv_actors = FB_ParsingUtils.Parse_tv_actor(filePath, -1);
		// System.out.println(tv_actors);

		UIUpdater.setWidgetText(label,
				DebugUtils.TimeToString((new Date()).getTime() - startTime));

	}

	private static void HandleTvPrograms(Display display, Label label) {
		String filePath = SharedPref.Tv_DB_Folder + SharedPref.fileSeparator
				+ "tv_program.tsv";

		long startTime = System.currentTimeMillis();
		tvPrograms tv_programs = FB_ParsingUtils
				.Parse_tv_programs(filePath, -1);
		// System.out.println(tv_programs);

		UIUpdater.setWidgetText(label,
				DebugUtils.TimeToString((new Date()).getTime() - startTime));

	}

}
